//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.imagemanager.util;

import android.graphics.Bitmap;
import android.net.Uri;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

public class Utils {
    private static final long POLY64REV = -7661587058870466123L;
    private static final long INITIALCRC = -1L;
    private static boolean init = false;
    private static long[] CRCTable = new long[256];

    public Utils() {
    }

    public static final void writeUTF(DataOutputStream dos, String string) throws IOException {
        if(string == null) {
            dos.writeUTF(new String());
        } else {
            dos.writeUTF(string);
        }

    }

    public static final String readUTF(DataInputStream dis) throws IOException {
        String retVal = dis.readUTF();
        return retVal.length() == 0?null:retVal;
    }

    public static final Bitmap resizeBitmap(Bitmap bitmap, int maxSize) {
        int srcWidth = bitmap.getWidth();
        int srcHeight = bitmap.getHeight();
        int width = maxSize;
        int height = maxSize;
        boolean needsResize = false;
        if(srcWidth > srcHeight) {
            if(srcWidth > maxSize) {
                needsResize = true;
                height = maxSize * srcHeight / srcWidth;
            }
        } else if(srcHeight > maxSize) {
            needsResize = true;
            width = maxSize * srcWidth / srcHeight;
        }

        if(needsResize) {
            Bitmap retVal = Bitmap.createScaledBitmap(bitmap, width, height, true);
            return retVal;
        } else {
            return bitmap;
        }
    }

    public static final long Crc64Long(String in) {
        if(in != null && in.length() != 0) {
            long crc = -1L;
            int i;
            int j;
            if(!init) {
                for(i = 0; i < 256; ++i) {
                    long part = (long)i;

                    for(j = 0; j < 8; ++j) {
                        int value = (int)part & 1;
                        if(value != 0) {
                            part = part >> 1 ^ -7661587058870466123L;
                        } else {
                            part >>= 1;
                        }
                    }

                    CRCTable[i] = part;
                }

                init = true;
            }

            i = in.length();

            for(j = 0; j < i; ++j) {
                char c = in.charAt(j);
                crc = CRCTable[((int)crc ^ c) & 255] ^ crc >> 8;
            }

            return crc;
        } else {
            return 0L;
        }
    }

    public static final String Crc64(String in) {
        if(in == null) {
            return null;
        } else {
            long crc = Crc64Long(in);
            int low = (int)crc & -1;
            int high = (int)(crc >> 32) & -1;
            String outVal = Integer.toHexString(high) + Integer.toHexString(low);
            return outVal;
        }
    }

    public static String getBucketNameFromUri(Uri uri) {
        String string = "";
        if(string == null || string.length() == 0) {
            List<String> paths = uri.getPathSegments();
            int numPaths = paths.size();
            if(numPaths > 1) {
                string = (String)paths.get(paths.size() - 2);
            }

            if(string == null) {
                string = "";
            }
        }

        return string;
    }

    public static void Copy(File src, File dst) throws IOException {
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);
        byte[] buf = new byte[1024];

        int len;
        while((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        in.close();
        out.close();
    }
}
